﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using MShop.Core.DataAccess;
using MShop.Web.Models;
using MShop.Core.Extensions;
using MShop.Core.Domain;
using MShop.Core.Service.Membership;
using MShop.Core.Service.EmailSender;
using MShop.Core;

namespace MShop.Web.Areas.Admin.Controllers
{
    public class SendMailController : SecureController
    {
        private readonly IRoleService _RoleService;
        private readonly IEmailSender _EmailSender;
        private readonly IShopContext _ShopContext;
        public SendMailController( IRoleService roleService,IEmailSender emailSender, IShopContext shopContext)
        {
            _RoleService = roleService;
            _EmailSender = emailSender;
            _ShopContext = shopContext;
            _EmailSender.SendCompleted += new System.Net.Mail.SendCompletedEventHandler(_EmailSender_SendCompleted);
        }

        void _EmailSender_SendCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            if (e.Cancelled)
            {

            }
            if (e.Error != null)
            {

            }
            //完成
            
        }
        
        public ActionResult Send()
        {
            ViewData["roles"] = _RoleService.FindRoles().ConvertListItem("RoleName", "RoleId");
            return View(new SendMailModel());
        }


        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Send(SendMailModel model,FormCollection form)
        {
            int successamount = 0;
              ViewData["roles"] = _RoleService.FindRoles().ConvertListItem("RoleName", "RoleId",model.RoleId);
              if (Request.Params["sendtype"] == "1")//用户
              {
                  if (!string.IsNullOrEmpty(model.Emails))
                  {
                      string[] emails = new string[] { };
                      if (model.Emails.IndexOf(',') != -1)
                      {
                          emails = model.Emails.Split(',');
                      }
                      else
                      {
                          emails = new string[] { model.Emails };
                      }
                      foreach (string email in emails)
                      {
                          System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(_ShopContext.Settings.Email, email, model.Subject, model.Body);
                          message.IsBodyHtml = true;
                          message.SubjectEncoding = System.Text.Encoding.UTF8;
                          message.BodyEncoding = System.Text.Encoding.UTF8;                         
                          try
                          {
                              _EmailSender.Send(message);
                              successamount++;
                          }
                          catch
                          {
                          }
                      }
                  }
                  ViewData["msg"]="发送成功,"+successamount.ToString()+"成功.";
              }
              else//角色
              {
                  ViewData["msg"] = "角色暂时不支持发送";
              }
              
              return View(model);
        }
    }
}